package cn.softcits.network.one;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @author Rocket
 * @version 10.0.1
 * @date 2021-07-18 2:00 PM
 */
public class ServerThread implements Runnable {

    private Socket s;
    private BufferedReader br;

    public ServerThread(Socket s) throws Exception {
        this.s = s;
        br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        System.out.println(s.getInetAddress().getHostAddress() + " connected.");
    }

    @Override
    public void run() {

        String content;
        try {
            String add = this.s.getInetAddress().getHostAddress();
            System.out.println(add + " is connected.");
//            while((content = br.readLine())!= null) {
//                for (Socket s : Server.socketList)
//                {
//                    PrintStream ps = new PrintStream(s.getOutputStream());
//                    ps.println(content);
//                }
//                System.out.println(add + ": " + content);
//
//            }

            PrintWriter out = new PrintWriter(s.getOutputStream(), true);
            out.write("HTTP/1.1 200 OK\n");
            out.write("Server:apache-Coyote/1.2\n");
            out.write("Content-Type:text/html;charset=UTF-8\n");
            out.write("\r\n");
            out.write("Happy birthday, 我的浏览器.\r\n");

            out.write("<table border='1' style='font-size: 18px;'><tr><td>123</td><td>234</td></tr>\r\n");
            out.write("<tr><td>123</td><td>234</td></tr>\r\n");
            out.write("<tr><td>123</td><td>234</td></tr>\r\n");
            out.write("<tr><td>123</td><td>234</td></tr></table>\r\n");

            out.write("<a href='https://www.baidu.com'>click me</a>\r\n");

            out.close();
            br.close();
            //s.close();
        } catch (Exception e) {

        }

    }
}
